package com.luoxiang.socialandroid.adapter;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.CircleCrop;
import com.bumptech.glide.request.RequestOptions;
import com.luoxiang.socialandroid.R;
import com.luoxiang.socialandroid.app.MyAppliaction;
import com.luoxiang.socialandroid.bean.Operation;
import com.luoxiang.socialandroid.utils.CustomClickListener;

import java.util.ArrayList;
import java.util.List;

public class OperationsAdapter extends RecyclerView.Adapter<OperationsAdapter.ViewHolder> {
    private String url = "https://dahuixiong.oss-cn-shenzhen.aliyuncs.com/";
    private List<Operation> list = new ArrayList<>();

    public void setData(List<Operation> list) {
        this.list = list;
    }

    public static interface OnAddClickListener {
        // true add; false cancel
        void onItemClicks(int userid); //传递boolean类型数据给activity
    }

    public static interface OnAddClickListenerDs {
        // true add; false cancel
        void onItemClicksDs(int position, int userid, int id);
    }

    public static interface OnAddClickListenerSx {

        void onItemClicksSx(int userid);
    }

    public static interface OnAddClickListenerSp {

        void onItemClicksSp(int userid);
    }

    OnAddClickListener onItemAddClick;

    OnAddClickListenerDs onItemAddClickDs;

    OnAddClickListenerSx onItemAddClickSx;

    OnAddClickListenerSp onItemAddClickSp;

    public void setOnAddClickListener(OnAddClickListener onItemAddClick) {
        this.onItemAddClick = onItemAddClick;
    }

    public void setOnAddClickListenerDs(OnAddClickListenerDs onItemAddClickDs) {
        this.onItemAddClickDs = onItemAddClickDs;
    }

    public void setOnAddClickListenerSx(OnAddClickListenerSx onItemAddClickSx) {
        this.onItemAddClickSx = onItemAddClickSx;
    }

    public void setOnAddClickListenerSp(OnAddClickListenerSp onItemAddClickSp) {
        this.onItemAddClickSp = onItemAddClickSp;
    }

    @NonNull
    @Override
    public OperationsAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.item_op, parent, false);
        OperationsAdapter.ViewHolder holder = new OperationsAdapter.ViewHolder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(@NonNull OperationsAdapter.ViewHolder holder, int position) {
        Operation bean = list.get(position);
        Glide.with(MyAppliaction.getContext())
                .load(url + bean.getIcon())
                .apply(RequestOptions.bitmapTransform(new CircleCrop()))
                .into(holder.im1_op);
        if (bean.getIdentity() == 10) {
            holder.im2_op.setImageResource(R.mipmap.icon_huangjin);
            holder.im2_op.setVisibility(View.VISIBLE);
        } else if (bean.getIdentity() == 20) {
            holder.im2_op.setImageResource(R.mipmap.icon_bojin);
            holder.im2_op.setVisibility(View.VISIBLE);
        } else if (bean.getIdentity() == 30) {
            holder.im2_op.setImageResource(R.mipmap.icon_zuanshi);
            holder.im2_op.setVisibility(View.VISIBLE);
        } else {
            holder.im2_op.setVisibility(View.GONE);
        }
        if (bean != null) {
            if (bean.getInterval() != null) {
                holder.tvZuijin.setText(bean.getInterval());
            }
        }
        holder.tx_zhuce.setVisibility(View.GONE);
        holder.tx_huoyue.setVisibility(View.GONE);
        holder.tx_tonghua.setVisibility(View.GONE);
        holder.tx_chongzhi.setVisibility(View.GONE);
        holder.tx1_op.setText(bean.getName());
        holder.tx2_op.setText(String.valueOf(bean.getAge()));
//        if (bean.getOperation().equals(holder.tx2_op.getContext().getString(R.string.mi_chongzhi))) {
//            holder.tx_chongzhi.setVisibility(View.VISIBLE);
//        } else if (bean.getOperation().equals(holder.tx2_op.getContext().getString(R.string.mi_tonghua))) {
//            holder.tx_tonghua.setVisibility(View.VISIBLE);
//        } else if (bean.getOperation().equals(holder.tx2_op.getContext().getString(R.string.mi_zhuce))) {
//            holder.tx_zhuce.setVisibility(View.VISIBLE);
//        } else if (bean.getOperation().equals(holder.tx2_op.getContext().getString(R.string.mi_huoyue))) {
//            holder.tx_huoyue.setVisibility(View.VISIBLE);
//        }else {
//            holder.tx_zhuce.setVisibility(View.GONE);
//            holder.tx_huoyue.setVisibility(View.GONE);
//            holder.tx_tonghua.setVisibility(View.GONE);
//            holder.tx_chongzhi.setVisibility(View.GONE);
//        }
        if (bean.getOperation() != null) {
            holder.tx_chongzhi.setText(bean.getOperation());
            holder.tx_chongzhi.setVisibility(View.VISIBLE);
        }else {
            holder.tx_chongzhi.setVisibility(View.GONE);
        }
        holder.im1_op.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                onItemAddClick.onItemClicks(bean.getUserId());
            }
        });
        if (bean.getGender() == 0) {
            holder.tx2_op.setBackgroundResource(R.drawable.shape_girlbg);
            holder.img_logosex.setImageResource(R.mipmap.icon_nansheng);

        } else {
            holder.tx2_op.setBackgroundResource(R.drawable.shape_tc_txbg);
            holder.img_logosex.setImageResource(R.mipmap.icon_nvsheng);
        }
        if (bean.getCall() == 1) {
            holder.re_dashan.setVisibility(View.GONE);
            holder.im_sixin.setVisibility(View.VISIBLE);
        } else {
            holder.re_dashan.setVisibility(View.VISIBLE);
            holder.im_sixin.setVisibility(View.GONE);
        }
        holder.re_dashan.setOnClickListener(new CustomClickListener() {
            @Override
            protected void onSingleClick() {
                onItemAddClickDs.onItemClicksDs(position, bean.getUserId(), bean.getId());
                bean.setCall(1);
                notifyItemChanged(position);
            }

            @Override
            protected void onFastClick() {

            }
        });
        holder.im_sixin.setOnClickListener(new CustomClickListener() {
            @Override
            protected void onSingleClick() {
                onItemAddClickSx.onItemClicksSx(bean.getUserId());

            }

            @Override
            protected void onFastClick() {

            }
        });
        holder.shipin.setOnClickListener(new CustomClickListener() {
            @Override
            protected void onSingleClick() {
                //点击视频
                onItemAddClickSp.onItemClicksSp(bean.getUserId());
            }

            @Override
            protected void onFastClick() {

            }
        });
    }

    @Override
    public int getItemCount() {
        try {
            return list.size();
        } catch (Exception e) {
            return 0;
        }
    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        private ImageView im1_op, im2_op, im_sixin, img_logosex;
        private RelativeLayout re_dashan;
        private TextView tx1_op, tx2_op, tx_chongzhi, tx_huoyue, tx_tonghua, tx_zhuce;
        private ImageView shipin;
        private TextView tvZuijin;
        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            im1_op = itemView.findViewById(R.id.im1_op);
            im2_op = itemView.findViewById(R.id.im2_op);
            tx1_op = itemView.findViewById(R.id.tx1_op);
            tx2_op = itemView.findViewById(R.id.tx2_op);
            img_logosex = itemView.findViewById(R.id.img_logosex);
            im_sixin = itemView.findViewById(R.id.im_sixin);
            re_dashan = itemView.findViewById(R.id.re_dashan);
            tx_chongzhi = itemView.findViewById(R.id.tx_chongzhi);
            tx_huoyue = itemView.findViewById(R.id.tx_huoyue);
            tx_tonghua = itemView.findViewById(R.id.tx_tonghua);
            tx_zhuce = itemView.findViewById(R.id.tx_zhuce);
            shipin = itemView.findViewById(R.id.shipin);
            tvZuijin=itemView.findViewById( R.id.tvZuijin);
        }
    }
}
